﻿using ManageLiteAV;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TRTCCSharpDemo
{
    public partial class TRTCClassForm : Form
    {
        private ITRTCCloud mTRTCCloud;
        // 窗口实例
        private TRTCLoginForm mLoginForm;
        //给定一个flag防止绑定数据源的时候就触发  SelectedIndexChanged方法
        bool flag = false;

        public TRTCClassForm()
        {
            InitializeComponent();
            // 窗口退出事件监听
            this.FormClosing += new FormClosingEventHandler(OnExitLabelClick);
        }

        public TRTCClassForm(TRTCLoginForm loginForm)
        {
            InitializeComponent();
            this.mLoginForm = loginForm;
            mTRTCCloud = DataManager.GetInstance().trtcCloud;
            // 窗口退出事件监听
            this.FormClosing += new FormClosingEventHandler(OnExitLabelClick);
        }
        
        
        private void OnExitLabelClick(object sender, EventArgs e)
        {
            // 退出房间
            if (mLoginForm == null)
                mLoginForm = new TRTCLoginForm();
            mLoginForm.Show();
        }
        //初始化页面数据
        private void TRTCClassForm_Load(object sender, EventArgs e)
        {
            string strGradeInfo = "";
            strGradeInfo = DataManager.GetInstance().gradeInfo;
            int class_type = DataManager.GetInstance().classType;
            JObject jo = (JObject)JsonConvert.DeserializeObject(strGradeInfo);
            JArray jsonAry = JArray.Parse(jo["msg"].ToString());
            //string msg = jo["msg"].ToString();
            //绑定下拉框列表数据源
            Dictionary<int, string> kvDictonary = new Dictionary<int, string>();
            kvDictonary.Add(0, "请选择授课课堂");
            for (int i = 0; i < jsonAry.Count; i++) {
                JObject classInfo = JObject.Parse(jsonAry[i].ToString());
                //如果课程为正式课 下拉框数据
                if (class_type == 1)
                {
                    int id = int.Parse(classInfo["id"].ToString());
                    string grades_time = classInfo["grades_time"].ToString();
                    string grade_size = classInfo["grade_size"].ToString();
                    string course_name = classInfo["grades_name"].ToString();
                    string course_info = course_name + "--" + grades_time + "--" + grade_size + "人";
                    kvDictonary.Add(id, course_info);
                }
                //体验课堂 下拉框
                else if (class_type == 2) {
                    int id = int.Parse(classInfo["id"].ToString());
                    string course_name = classInfo["course_name"].ToString();
                    kvDictonary.Add(id, course_name);
                }
                
            }
            //给下拉框绑定数据源 
            BindingSource bs = new BindingSource();
            bs.DataSource = kvDictonary;
            this.classSelectBox.DataSource = bs;
            this.classSelectBox.ValueMember = "Key";
            this.classSelectBox.DisplayMember = "Value";
            //绑定完之后设置为true
            flag = true;
        }
        //下拉框值改变事件
        private void classSelectBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flag) {
                uint index = uint.Parse(this.classSelectBox.SelectedValue.ToString());
                //绑定房间号信息
                DataManager.GetInstance().roomId = index;
            }
            
        }
        //进入课堂点击按钮事件
        private void enterClassBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.mLoginForm.Hide();
            TRTCMainForm mainForm = new TRTCMainForm(this);
            mainForm.Show();
            mainForm.EnterRoom();
        }
    }
}
